// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Restore building / compositions that have been backup from Merlin
//             Position is the center of composition
// -----------------------------------------------

private [
	"_typeof",
	"_grouped",
	"_markername",
	"_marker",
	"_scriptinit",
	"_leader",
	"_wp",
	"_group",
	"_soldier",
	"_building",
	"_attitude",
	"_dir",
	"_markersize",
	"_position",
	"_triggerside",
	"_triggerobjective",
	"_triggersize",
	"_trg",
	"_type",
	"_arrayof",
	"_withmen"
];

_arrayof = _this select 0;
_withmen = _this select 1;

{
	_type = _x select 0;
	switch (toLower _type) do {
		case "create": {
			_attitude = (_x select 2) select 2;
			if (count ((_x select 2) select 0) > 1) then {
				_typeof = (_x select 2) select 0;
				_grouped = true;
			} else {
				_typeof = ((_x select 2) select 0) select 0;
				_grouped = false;
			};
			_dir = (_x select 2) select 3;
			_markersize = (_x select 2) select 4;
			_position = _x select 3;

			//A3 TODO: setVehicleInit
			if (_grouped) then {
				_group = [_position, east, _typeof, [], [], [wcskill,wcskill,wcskill]] call BIS_fnc_spawnGroup;
				switch (toLower _attitude) do {
					case "ups": {
						_markername = format ["merlin%1ups", wcmerlinmrk];
						wcmerlinmrk = wcmerlinmrk + 1;
						_marker = [_markername, _markersize, _position, "Default", "ICON", "FDIAGONAL", "EMPTY"] call WC_fnc_createmarker;
						_scriptinit = format ["wcgarbage = [this, '%1', 'noslow'] execVM 'extern\ups.sqf';", _markername];
						_leader = leader _group;
						_leader setVehicleInit _scriptinit;
						processInitCommands;
					};

					case "s&d area": {
						_markername = format ["merlin%1ups", wcmerlinmrk];
						wcmerlinmrk = wcmerlinmrk + 1;
						_marker = [_markername, _markersize, _position, "Default", "ICON", "FDIAGONAL", "EMPTY"] call WC_fnc_createmarker;
						wcgarbage = [_group, _markername] spawn {
							private ["_group","_position","_markername","_waypoint"];
							_group      = _this select 0;
							_markername = _this select 1;
							while {count (units _group) > 1} do {
								_position = [_markername] call WC_fnc_createpositioninmarker;
								_waypoint = _group addWaypoint [_position, 0];
								[_group, 0] setWaypointType "SAD";
								sleep 120;
								deleteWaypoint [_group, 0];
							};
						};
					};

					case "guard": {
						_wp = _group addWaypoint [getPosATL (leader _group), 0];
						[_group, 0] setWaypointType "GUARD";
					};
	
					default {};
				};
			} else {
				_building = _typeof createVehicle _position;
				_building setPosATL _position;
				_building setDir _dir;
				_building setVectorUp [0,0,1];
				_building addEventHandler ["Killed", {wcgarbage = _this spawn WC_fnc_garbagecollector}];
				if (_withmen) then {
					_group = createGroup east;
					if (_building emptyPositions "driver" > 0) then {
						_soldier = _group createUnit [(wccrewforces call BIS_fnc_selectRandom), _position, [], 0, "NONE"];
						_soldier assignAsDriver _building;
						_soldier moveInDriver _building;
						wcgarbage = [_soldier, wcskill] spawn WC_fnc_setskill;
						_soldier addEventHandler ["Killed", {wcgarbage = _this spawn WC_fnc_garbagecollector}];
					};
					if (_building emptyPositions "gunner" > 0) then {
						_soldier = _group createUnit [(wccrewforces call BIS_fnc_selectRandom), _position, [], 0, "NONE"];
						_soldier assignAsGunner _building;
						_soldier moveInGunner _building;
						wcgarbage = [_soldier, wcskill] spawn WC_fnc_setskill;
						_soldier addEventHandler ["Killed", {wcgarbage = _this spawn WC_fnc_garbagecollector}];
					};
					if (_building emptyPositions "commander" > 0) then {
						_soldier = _group createUnit [(wccrewforces call BIS_fnc_selectRandom), _position, [], 0, "NONE"];
						_soldier assignAsCommander _building;
						_soldier moveInCommander _building;
						wcgarbage = [_soldier, wcskill] spawn WC_fnc_setskill;
						_soldier addEventHandler ["Killed", {wcgarbage = _this spawn WC_fnc_garbagecollector}];
					};
					_markername = format ["merlin%1ups", wcmerlinmrk];
					wcmerlinmrk = wcmerlinmrk + 1;
					_marker = [_markername, _markersize, _position, "Default", "ICON", "FDIAGONAL", "EMPTY"] call WC_fnc_createmarker;
					_scriptinit = format ["wcgarbage = [this, '%1', 'noslow'] execVM 'extern\ups.sqf';", _markername];
					_leader = leader _group;
					_leader setVehicleInit _scriptinit;
					processInitCommands;
				};
			};
		};

		case "area objective": {
			_triggerside = (_x select 2) select 1;
			_triggerobjective = (_x select 2) select 2;
			_triggersize = (_x select 2) select 5;
			_position = _x select 3;
		
			_trg = createTrigger ["EmptyDetector", _position]; 
			_trg setTriggerArea [_triggersize, _triggersize, 0, false];
			_trg setTriggerActivation [_triggerside, _triggerobjective, false];
			_trg setTriggerStatements ["this", "
				wcmissionokW = true; 
				publicVariable 'wcmissionokW'; 
				wcgarbage = [nil, nil, rHINT,'Mission done!'] call RE;
				wcmissionclear = true;
			", ""];
		};

		case "select map object": {
			_position = _x select 3;
			wcgarbage = ["Destroy", 0.5, _position, "ColorRed", "ICON", "FDIAGONAL", "Flag", 0, "Destroy"] call WC_fnc_createmarker;
			_building = (_position nearObjects 5) select 0;
			wcgarbage = [_building] spawn {
				private ["_building","_missionend"];
				_building = _this select 0;
				_missionend = false;
				while {!_missionend} do {
					if (getDammage _building > 0.9) then {
						wcmissionokW = true; 
						publicVariable "wcmissionokW"; 
						wcgarbage = [nil, nil, rHINT, "Mission done!"] call RE;
						wcmissionclear = true;
						_missionend = true;
					};
					sleep 1;
				};
			};
		};

		case "void": {

		};

		default {};
	};
	sleep 0.1;
} foreach _arrayof;

_group;